package com.sunnyweather.android.ui.place;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sunnyweather.android.R;
import com.sunnyweather.android.SunnyWeatherApplication;
import com.sunnyweather.android.logic.model.PlaceResponse;
import com.sunnyweather.android.logic.network.PlaceService;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Retrofit;

public class PlaceFragment extends Fragment {
    private List<PlaceResponse.Place> places = new ArrayList<>();
    private PlaceAdapter adapter;
    private RecyclerView recyclerView;
    private EditText search;

    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_place, container, false);
    }


    @Override
    public void onActivityCreated(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        search = getActivity().findViewById(R.id.searchPlaceEdit);


        places.add(new PlaceResponse.Place("中国", new PlaceResponse.Location("AA", "BB")));
        places.add(new PlaceResponse.Place("中国", new PlaceResponse.Location("AA", "BB")));
        places.add(new PlaceResponse.Place("中国", new PlaceResponse.Location("AA", "BB")));
        places.add(new PlaceResponse.Place("中国", new PlaceResponse.Location("AA", "BB")));
        places.add(new PlaceResponse.Place("中国", new PlaceResponse.Location("AA", "BB")));
        places.add(new PlaceResponse.Place("中国", new PlaceResponse.Location("AA", "BB")));
        places.add(new PlaceResponse.Place("中国", new PlaceResponse.Location("AA", "BB")));
        places.add(new PlaceResponse.Place("中国", new PlaceResponse.Location("AA", "BB")));
        places.add(new PlaceResponse.Place("中国", new PlaceResponse.Location("AA", "BB")));
        places.add(new PlaceResponse.Place("中国", new PlaceResponse.Location("AA", "BB")));
        places.add(new PlaceResponse.Place("中国", new PlaceResponse.Location("AA", "BB")));

        adapter = new PlaceAdapter(this, places);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        recyclerView = getActivity().findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(adapter);

        search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String content = s.toString();
                if (!content.equals("")) {
                    adapter.notifyDataSetChanged();
                    Toast.makeText(SunnyWeatherApplication.context, "执行搜索", Toast.LENGTH_SHORT).show();
                    recyclerView.setVisibility(View.VISIBLE);
                } else {
                    recyclerView.setVisibility(View.GONE);
//                    places.clear();
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }


}
